package cn.demarcia.coco.ui.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.demarcia.coco.AppConfig;
import cn.demarcia.coco.R;
import cn.demarcia.coco.uihelper.StringUtils;
import info.jimao.newsdk.models.JokeModel;

/**
 * Created by lzy
 * on 2016/8/9.
 */
public class JokeAdapter extends RecyclerView.Adapter<JokeAdapter.ViewHolder> {
    private List<JokeModel.JokeInfo> items = new ArrayList<>();
    private Context context;
    private final LayoutInflater mLayoutInflater;
    private String type;

    public JokeAdapter(Context context, List<JokeModel.JokeInfo> mItems, String type) {
        this.context = context;
        this.items = mItems;
        this.type = type;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mLayoutInflater.inflate(R.layout.list_item_joke, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final JokeModel.JokeInfo item = items.get(position);
        if (!StringUtils.isEmpty(type) && type.equals(AppConfig.JOKE_TEXT)) {
            holder.llImgHolder.setVisibility(View.GONE);
            holder.tvContent.setText(item.content);
            holder.tvUpdateTime.setText(item.updatetime);
        } else {
            holder.llTextHolder.setVisibility(View.GONE);
            holder.tvUpdate.setText(item.updatetime);
            holder.tvTitle.setText(item.content);
            holder.ivPic.setImageURI(Uri.parse(item.url));
        }
    }

    @Override
    public int getItemCount() {
        return items == null ? 0 : items.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.llTextHolder)
        LinearLayout llTextHolder;
        @Bind(R.id.llImgHolder)
        LinearLayout llImgHolder;
        @Bind(R.id.tvTitle)
        TextView tvTitle;
        @Bind(R.id.tvUpdateTime)
        TextView tvUpdateTime;
        @Bind(R.id.tvUpdate)
        TextView tvUpdate;
        @Bind(R.id.tvContent)
        TextView tvContent;
        @Bind(R.id.ivPic)
        SimpleDraweeView ivPic;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
